package com.anpy.bill.controller;

import com.anpy.bill.model.Account;
import com.anpy.bill.model.PageResult;
import com.anpy.bill.service.AccountService;
import com.anpy.common.service.model.result.ApiResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/accounts")
@ResponseBody
public class AccountController {
    private final AccountService accountService;

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @PostMapping
    public ApiResult createAccount(@RequestBody Account account, @RequestParam int userId) {
        return accountService.createAccount(account, userId);
    }

    @GetMapping("/{id}")
    public ApiResult<Account> getAccount() {
        return ApiResult.success(accountService.getAccount(1L, 1));
    }

    @GetMapping
    public ApiResult<PageResult<Account>> getAccountsByUserId(
        @RequestParam int userId,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size) {
        return ApiResult.success(accountService.getAccountsByUserId(userId, page, size));
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateAccount(@PathVariable Long id, @RequestBody Account account, @RequestParam int userId) {
        return accountService.updateAccount(id, account, userId);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteAccount(@PathVariable Long id, @RequestParam int userId) {
        return accountService.deleteAccount(id, userId);
    }
}
